﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JobbJakt
{
    public partial class Advertisement
    {

        /// <summary>
        /// Lägger till en annons
        /// </summary>
        /// <param name="advertisement">Tar in en Annons av typen Advertisement</param>
        /// <returns></returns>
        public static void AddAdvertisement(Advertisement advertisement)
        {
            var query = JobbJaktDB.Advertisements.Add(advertisement);
            JobbJaktDB.Save();
        }

        public static TypeOfEmployment GetTypeOfEmployment(int i)
        {
            if (i == 0)
            {
                return JobbJakt.TypeOfEmployment.Praktik;
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        /// Tar bort en annons
        /// </summary>
        /// <param name="advertisementid">ID för annonsen som ska tas bort</param>
        public static int RemoveAdvertisement(int advertisementid)
        {
            var query = JobbJaktDB.Advertisements
                .Where(a => a.AdvertisementID == advertisementid)
                .Select(a => a.Active == false).First();
            return JobbJaktDB.Save();
        }

        /// <summary>
        /// Hämtar en annons
        /// </summary>
        /// <param name="advertisementid">ID på annonsen som du vill hämta</param>
        /// <returns>Annonsen</returns>
        public static Advertisement GetAdvertisement(int advertisementid)
        {
            var query = JobbJaktDB.Advertisements
                .SingleOrDefault(a => a.AdvertisementID == advertisementid);
            return query;
        }

        /// <summary>
        /// Hämtar alla aktiva annonser
        /// </summary>
        /// <returns>En lista av annonser</returns>
        public static List<Advertisement> GetAdvertisements()
        {
            var query = JobbJaktDB.Advertisements
                .Where(a => a.Active == true).ToList();
            return query;
        }

        /// <summary>
        /// Konstruktor som tar in alla parametrar
        /// </summary>
        public Advertisement(int advertisementid, DateTime registrationdate, bool active, string title, string position, string description,
            DateTime expiration, string qualifications, string category, int salary, TypeOfEmployment typeofemployment, int companycontactid)
        {
            AdvertisementID = advertisementid;
            RegistrationDate = registrationdate;
            Active = active;
            Title = title;
            Position = position;
            Description = description;
            ExpirationDate = expiration;
            Qualifications = qualifications;
            Category = category;
            Salary = salary;
            TypeOfEmployment = typeofemployment;
            CompanyContactID = companycontactid;
        }

        /// <summary>
        /// Konstruktor som tar in alla parametrar utom salary (för att salary är nullable)
        /// </summary>
        public Advertisement(int advertisementid, DateTime registrationdate, bool active, string title, string position, string description,
            DateTime expiration, string qualifications, string category, TypeOfEmployment typeofemployment, int companycontactid)
        {
            AdvertisementID = advertisementid;
            RegistrationDate = registrationdate;
            Active = active;
            Title = title;
            Position = position;
            Description = description;
            ExpirationDate = expiration;
            Qualifications = qualifications;
            Category = category;
            TypeOfEmployment = typeofemployment;
            CompanyContactID = companycontactid;
        }

        /// <summary>
        /// Kollar om salary är null eller ej och returnerar därefter korrekt string.
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if (Salary == null)
                return string.Format("Annons-ID:{0}, Registreringsdatum:{1}, Titel:{2}, Befattning:{3}, Utgångsdatum:{4}, Kvalifikationer:{5}, Kategori:{6}, Typ av anställning:{7}, Företagets kontakt-ID:{8}, Beskrivning:{9}",
                    AdvertisementID, RegistrationDate, Title, Position, ExpirationDate, Qualifications, Category, TypeOfEmployment, CompanyContactID, Description);
            else
                return string.Format("Annons-ID:{0}, Registreringsdatum:{1}, Titel:{2}, Befattning:{3}, Utgångsdatum:{4}, Kvalifikationer:{5}, Kategori:{6}, Typ av anställning:{7}, Lön:{8}, Företagets kontakt-ID:{9}, Beskrivning:{10}",
                                    AdvertisementID, RegistrationDate, Title, Position, ExpirationDate, Qualifications, Category, TypeOfEmployment, Salary, CompanyContactID, Description);
        }


    }
}
